﻿using EasyERP_BLL;
using EasyERP_BLL.Base;
using EasyERP_Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace EasyERP_View.FrmBase
{
    /// <summary>
    /// 人员信息管理窗体
    /// </summary>
    public partial class FrmUser : DockContent
    {
        /// <summary>
        /// 声明人员信息业务逻辑类对象
        /// </summary>
        private readonly B_User_BLL User_BLL = new B_User_BLL();

        /// <summary>
        /// 声明存储DataGridView单元格中旧的数据
        /// </summary>
        private string str_old;

        /// <summary>
        /// 声明存储DataGridView单元格中新的的数据
        /// </summary>
        private string str_new;

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmUser()
        {
            InitializeComponent();
        }
        #endregion

        #region 单击查询按钮事件
        /// <summary>
        /// 单击查询按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSearch_Click(object sender, EventArgs e)
        {
            //判断是否有未保存数据
            if (BeforSave())
            {
                DialogResult result = B_Message_BLL.MessageYesOrNo("2100");
                if (result == DialogResult.Yes)
                {
                    Query();
                }
            }
            else
            {
                Query();
            }
        }
        #endregion

        #region 单击添加行按钮事件
        /// <summary>
        /// 单击添加行按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnAddRow_Click(object sender, EventArgs e)
        {
            //判断DataGridView是否为空
            if (DGView.DataSource == null)
            {
                DGView.Rows.Add();
            }
            else
            {
                ((DataTable)DGView.DataSource).Rows.Add();
            }

            //修改行首图标
            DGView.Rows[DGView.Rows.Count - 1].Cells["idu"].Value = Properties.Resources.I;
            DGView.Rows[DGView.Rows.Count - 1].Cells["idu"].ToolTipText = "I";
        }
        #endregion

        #region 单击删除行按钮事件
        /// <summary>
        /// 单击删除行按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnDelRow_Click(object sender, EventArgs e)
        {
            //遍历所有选中的行
            foreach (DataGridViewRow row in DGView.SelectedRows)
            {
                //判断该行是否是新添加的行
                if (row.Cells["idu"].ToolTipText == "I")
                {
                    DGView.Rows.Remove(row);    //删除行
                }
                else//不是新添加的行
                {
                    //替换行首图标为删除图标
                    row.Cells["idu"].Value = Properties.Resources.D;
                    //修改鼠标悬停的提示符
                    row.Cells["idu"].ToolTipText = "D";
                }
            }
        }
        #endregion

        #region 当前选中单元格的编辑模式启动时事件
        /// <summary>
        /// 当前选中单元格的编辑模式启动时事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DGView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            str_old = string.Empty;     //清空数据
            //获取当前表格编辑前的数据
            str_old = DGView[e.ColumnIndex, e.RowIndex].Value == null ? "" : DGView[e.ColumnIndex, e.RowIndex].Value.ToString();
        }
        #endregion

        #region 当前选中单元格的编辑模式停止时事件
        /// <summary>
        /// 当前选中单元格的编辑模式停止时事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DGView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            str_new = string.Empty;     //清空数据
            //获取当前表格编辑结束的数据
            str_new = DGView[e.ColumnIndex, e.RowIndex].Value == null ? "" : DGView[e.ColumnIndex, e.RowIndex].Value.ToString();
            //判断编辑前的数据和编辑后的数据是否相同，并且是查询到的数据
            if (str_old != str_new && DGView.Rows[e.RowIndex].Cells["idu"].ToolTipText == "")
            {
                //修改行首图标
                DGView.Rows[e.RowIndex].Cells["idu"].Value = Properties.Resources.U;
                DGView.Rows[e.RowIndex].Cells["idu"].ToolTipText = "U";
            }
        }
        #endregion

        #region 单击保存按钮事件
        /// <summary>
        /// 单击保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSave_Click(object sender, EventArgs e)
        {
            //声明主代码实体对象
            B_User model = new B_User();
            foreach (DataGridViewRow row in DGView.Rows)
            {
                model.user_cd = row.Cells["user_cd"].Value == null ? "" : row.Cells["user_cd"].Value.ToString();
                model.user_nm = row.Cells["user_nm"].Value == null ? "" : row.Cells["user_nm"].Value.ToString();
                model.tel_no = row.Cells["tel_no"].Value == null ? "" : row.Cells["tel_no"].Value.ToString();
                model.dept_nm = row.Cells["dept_nm"].Value == null ? "" : row.Cells["dept_nm"].Value.ToString();
                model.remark = row.Cells["remark"].Value == null ? "" : row.Cells["remark"].Value.ToString();
                model.user_cd1 = FrmMain.user_cd;

                //判断每行需要调用的方法：增删改
                if (row.Cells["idu"].ToolTipText == "")      //没有修改
                {
                    continue;
                }
                else if (row.Cells["idu"].ToolTipText.ToUpper() == "I")     //新增
                {
                    if (!User_BLL.Insert_B_User(model))
                    {
                        return;
                    }
                }
                else if (row.Cells["idu"].ToolTipText.ToUpper() == "D")     //删除
                {
                    if (!User_BLL.Delete_B_User(model))
                    {
                        return;
                    }
                }
                else if (row.Cells["idu"].ToolTipText.ToUpper() == "U")     //修改
                {
                    if (!User_BLL.Updata_B_User(model))
                    {
                        return;
                    }
                }
                row.Cells["idu"].Value = Properties.Resources.Q;
                row.Cells["idu"].ToolTipText = "";
            }
            Query();
            B_Message_BLL.MessageCaveat("1005");
        }
        #endregion

        #region 当关闭主代码窗体时事件
        /// <summary>
        /// 当关闭主代码窗体时事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmUser_FormClosing(object sender, FormClosingEventArgs e)
        {
            //判断是否是用户关闭
            if (e.CloseReason == CloseReason.UserClosing)
            {
                //判断是否有未保存的数据
                if (BeforSave())        //有未保存数据
                {
                    DialogResult result = B_Message_BLL.MessageYesOrNo("2100");     //弹出提示框
                    if (result == DialogResult.Yes)
                    {
                        e.Cancel = false;       //关闭窗体
                    }
                    else
                    {
                        e.Cancel = true;        //不关闭窗体
                    }
                }
            }
        }
        #endregion

        #region 主代码查询方法
        /// <summary>
        /// 主代码查询方法
        /// </summary>
        private void Query()
        {
            //数据封装
            B_User model = new B_User
            {
                user_nm = Txt_User_nm.Text.Trim()
            };

            DataTable dt = User_BLL.Get_B_User(model);

            //绑定DataGridView
            DGView.DataSource = dt;

            //将主代码编号列设置为只读
            foreach (DataGridViewRow row in DGView.Rows)
            {
                row.Cells["user_cd"].ReadOnly = true;
            }

            if (dt.Rows.Count == 0)
            {
                B_Message_BLL.MessageCaveat("1004");
            }
        }
        #endregion

        #region 判断DataGridView中是否有尚未保存的数据
        /// <summary>
        /// 判断DataGridView中是否有尚未保存的数据
        /// </summary>
        /// <returns>返回True：有未保存的数据；返回False：没有未保存的数据</returns>
        private bool BeforSave()
        {
            foreach (DataGridViewRow row in DGView.Rows)
            {
                if (row.Cells["idu"].ToolTipText != "")
                {
                    return true;
                }
            }
            return false;
        }
        #endregion
    }
}
